package com.jinlia.show.modules.base.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.jinlia.show.common.core.utils.MybatisIdGenerator;
import com.jinlia.show.modules.base.config.bean.MybatisBaseTypeHandler;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {

    /**
     * 数据中心id,项目名
     */
    @Value("${date.center.id}")
    public String centerId;
    /**
     * 项目实例数
     */
    @Value("${date.center.worker}")
    public String worker;

    /**
     * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置
     * MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    /**
     * 图片地址转换
     *
     * @return
     */
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.getTypeHandlerRegistry().register(MybatisBaseTypeHandler.class);
    }

    @Bean
    public MybatisIdGenerator idGenerator() {
        return new MybatisIdGenerator(Long.valueOf("1288834974657"), Long.valueOf(worker), Long.valueOf(centerId));
    }
}
